﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LnSecurityLib;
using TestWindowsServices;
using System.Security.Principal;

namespace WindowsFormsApplication5
{

    public class ConexaoBD
    {
        public string strConexao { get; set; }
        public SqlConnection cn = new SqlConnection();
        public StringBuffer stringbuffer = new StringBuffer();


        private string strServer;
        private string strUserDB;
        private string strPasswordDB;
        private string strDatabase;
        private string bWindowsAutentication;
        public string strConnectionString;

        public void conectar()
        {

            try
            {
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Linx Sistemas\LinxPOS", false);
                
                strConnectionString = rk.GetValue("ConnectionString").ToString();
                strConnectionString = stringbuffer.LoadBuffer(stringbuffer.BuildBuffer(), strConnectionString);
            }
            catch (Exception erro)
            {

                Library.WriteErrorLog("Erro ao acessar informaoes do regedit: " + erro.Message);

            }
                //Library.WriteErrorLog(strConnectionString);

                string[] split = strConnectionString.Split(';');

                foreach (string s in split)
                {

                    if (s.Trim() != "")
                    strServer = split[0];
                    strDatabase = split[1];
                    strUserDB = split[2];
                    strPasswordDB = split[3];
                    bWindowsAutentication = split[4];
                }

                try
                {
                    if (bWindowsAutentication == "true")
                    {
                        strConexao = @"Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";Integrated Security=SSPI";
                        cn.ConnectionString = strConexao;
                        cn.Open();
                    }
                    else
                    {
                        strConexao = @"Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";User ID=" + strUserDB + ";Password=" + strPasswordDB;
                        cn.ConnectionString = strConexao;
                        cn.Open();
                    }

                    //cn.Open();
                    Library.WriteErrorLog("Conectou com sucesso !");

            }
            catch (Exception ex)
            {
                Library.WriteErrorLog("Erro ao efetuar a leitura do registro (regedit)" + "String de Conexão" + strConnectionString + ex.Message);
            }

        }

        public void Desconectar()
        {
            cn.Close();
            Library.WriteErrorLog("Desconectou com sucesso !");
        }


    }
}
